/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logica.jpaControllers;

import Entidades.Estadoespera;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Entidades.Transporte;
import java.util.ArrayList;
import java.util.Collection;
import Entidades.Detallerecepcion;
import logica.jpaControllers.exceptions.IllegalOrphanException;
import logica.jpaControllers.exceptions.NonexistentEntityException;

/**
 *
 * @author Anali
 */
public class EstadoesperaJpaController {

    public EstadoesperaJpaController() {
        emf = Persistence.createEntityManagerFactory("GestorPesajeJpaPU2");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Estadoespera estadoespera) {
        if (estadoespera.getTransporteCollection() == null) {
            estadoespera.setTransporteCollection(new ArrayList<Transporte>());
        }
        if (estadoespera.getDetallerecepcionCollection() == null) {
            estadoespera.setDetallerecepcionCollection(new ArrayList<Detallerecepcion>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Transporte> attachedTransporteCollection = new ArrayList<Transporte>();
            for (Transporte transporteCollectionTransporteToAttach : estadoespera.getTransporteCollection()) {
                transporteCollectionTransporteToAttach = em.getReference(transporteCollectionTransporteToAttach.getClass(), transporteCollectionTransporteToAttach.getIdTransporte());
                attachedTransporteCollection.add(transporteCollectionTransporteToAttach);
            }
            estadoespera.setTransporteCollection(attachedTransporteCollection);
            Collection<Detallerecepcion> attachedDetallerecepcionCollection = new ArrayList<Detallerecepcion>();
            for (Detallerecepcion detallerecepcionCollectionDetallerecepcionToAttach : estadoespera.getDetallerecepcionCollection()) {
                detallerecepcionCollectionDetallerecepcionToAttach = em.getReference(detallerecepcionCollectionDetallerecepcionToAttach.getClass(), detallerecepcionCollectionDetallerecepcionToAttach.getIdDetalleRecepcion());
                attachedDetallerecepcionCollection.add(detallerecepcionCollectionDetallerecepcionToAttach);
            }
            estadoespera.setDetallerecepcionCollection(attachedDetallerecepcionCollection);
            em.persist(estadoespera);
            for (Transporte transporteCollectionTransporte : estadoespera.getTransporteCollection()) {
                Estadoespera oldEstadoesperaOfTransporteCollectionTransporte = transporteCollectionTransporte.getEstadoespera();
                transporteCollectionTransporte.setEstadoespera(estadoespera);
                transporteCollectionTransporte = em.merge(transporteCollectionTransporte);
                if (oldEstadoesperaOfTransporteCollectionTransporte != null) {
                    oldEstadoesperaOfTransporteCollectionTransporte.getTransporteCollection().remove(transporteCollectionTransporte);
                    oldEstadoesperaOfTransporteCollectionTransporte = em.merge(oldEstadoesperaOfTransporteCollectionTransporte);
                }
            }
            for (Detallerecepcion detallerecepcionCollectionDetallerecepcion : estadoespera.getDetallerecepcionCollection()) {
                Estadoespera oldEstadoesperaOfDetallerecepcionCollectionDetallerecepcion = detallerecepcionCollectionDetallerecepcion.getEstadoespera();
                detallerecepcionCollectionDetallerecepcion.setEstadoespera(estadoespera);
                detallerecepcionCollectionDetallerecepcion = em.merge(detallerecepcionCollectionDetallerecepcion);
                if (oldEstadoesperaOfDetallerecepcionCollectionDetallerecepcion != null) {
                    oldEstadoesperaOfDetallerecepcionCollectionDetallerecepcion.getDetallerecepcionCollection().remove(detallerecepcionCollectionDetallerecepcion);
                    oldEstadoesperaOfDetallerecepcionCollectionDetallerecepcion = em.merge(oldEstadoesperaOfDetallerecepcionCollectionDetallerecepcion);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Estadoespera estadoespera) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Estadoespera persistentEstadoespera = em.find(Estadoespera.class, estadoespera.getIdEstadoEspera());
            Collection<Transporte> transporteCollectionOld = persistentEstadoespera.getTransporteCollection();
            Collection<Transporte> transporteCollectionNew = estadoespera.getTransporteCollection();
            Collection<Detallerecepcion> detallerecepcionCollectionOld = persistentEstadoespera.getDetallerecepcionCollection();
            Collection<Detallerecepcion> detallerecepcionCollectionNew = estadoespera.getDetallerecepcionCollection();
            List<String> illegalOrphanMessages = null;
            for (Transporte transporteCollectionOldTransporte : transporteCollectionOld) {
                if (!transporteCollectionNew.contains(transporteCollectionOldTransporte)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Transporte " + transporteCollectionOldTransporte + " since its estadoespera field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Transporte> attachedTransporteCollectionNew = new ArrayList<Transporte>();
            for (Transporte transporteCollectionNewTransporteToAttach : transporteCollectionNew) {
                transporteCollectionNewTransporteToAttach = em.getReference(transporteCollectionNewTransporteToAttach.getClass(), transporteCollectionNewTransporteToAttach.getIdTransporte());
                attachedTransporteCollectionNew.add(transporteCollectionNewTransporteToAttach);
            }
            transporteCollectionNew = attachedTransporteCollectionNew;
            estadoespera.setTransporteCollection(transporteCollectionNew);
            Collection<Detallerecepcion> attachedDetallerecepcionCollectionNew = new ArrayList<Detallerecepcion>();
            for (Detallerecepcion detallerecepcionCollectionNewDetallerecepcionToAttach : detallerecepcionCollectionNew) {
                detallerecepcionCollectionNewDetallerecepcionToAttach = em.getReference(detallerecepcionCollectionNewDetallerecepcionToAttach.getClass(), detallerecepcionCollectionNewDetallerecepcionToAttach.getIdDetalleRecepcion());
                attachedDetallerecepcionCollectionNew.add(detallerecepcionCollectionNewDetallerecepcionToAttach);
            }
            detallerecepcionCollectionNew = attachedDetallerecepcionCollectionNew;
            estadoespera.setDetallerecepcionCollection(detallerecepcionCollectionNew);
            estadoespera = em.merge(estadoespera);
            for (Transporte transporteCollectionNewTransporte : transporteCollectionNew) {
                if (!transporteCollectionOld.contains(transporteCollectionNewTransporte)) {
                    Estadoespera oldEstadoesperaOfTransporteCollectionNewTransporte = transporteCollectionNewTransporte.getEstadoespera();
                    transporteCollectionNewTransporte.setEstadoespera(estadoespera);
                    transporteCollectionNewTransporte = em.merge(transporteCollectionNewTransporte);
                    if (oldEstadoesperaOfTransporteCollectionNewTransporte != null && !oldEstadoesperaOfTransporteCollectionNewTransporte.equals(estadoespera)) {
                        oldEstadoesperaOfTransporteCollectionNewTransporte.getTransporteCollection().remove(transporteCollectionNewTransporte);
                        oldEstadoesperaOfTransporteCollectionNewTransporte = em.merge(oldEstadoesperaOfTransporteCollectionNewTransporte);
                    }
                }
            }
            for (Detallerecepcion detallerecepcionCollectionOldDetallerecepcion : detallerecepcionCollectionOld) {
                if (!detallerecepcionCollectionNew.contains(detallerecepcionCollectionOldDetallerecepcion)) {
                    detallerecepcionCollectionOldDetallerecepcion.setEstadoespera(null);
                    detallerecepcionCollectionOldDetallerecepcion = em.merge(detallerecepcionCollectionOldDetallerecepcion);
                }
            }
            for (Detallerecepcion detallerecepcionCollectionNewDetallerecepcion : detallerecepcionCollectionNew) {
                if (!detallerecepcionCollectionOld.contains(detallerecepcionCollectionNewDetallerecepcion)) {
                    Estadoespera oldEstadoesperaOfDetallerecepcionCollectionNewDetallerecepcion = detallerecepcionCollectionNewDetallerecepcion.getEstadoespera();
                    detallerecepcionCollectionNewDetallerecepcion.setEstadoespera(estadoespera);
                    detallerecepcionCollectionNewDetallerecepcion = em.merge(detallerecepcionCollectionNewDetallerecepcion);
                    if (oldEstadoesperaOfDetallerecepcionCollectionNewDetallerecepcion != null && !oldEstadoesperaOfDetallerecepcionCollectionNewDetallerecepcion.equals(estadoespera)) {
                        oldEstadoesperaOfDetallerecepcionCollectionNewDetallerecepcion.getDetallerecepcionCollection().remove(detallerecepcionCollectionNewDetallerecepcion);
                        oldEstadoesperaOfDetallerecepcionCollectionNewDetallerecepcion = em.merge(oldEstadoesperaOfDetallerecepcionCollectionNewDetallerecepcion);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = estadoespera.getIdEstadoEspera();
                if (findEstadoespera(id) == null) {
                    throw new NonexistentEntityException("The estadoespera with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Estadoespera estadoespera;
            try {
                estadoespera = em.getReference(Estadoespera.class, id);
                estadoespera.getIdEstadoEspera();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The estadoespera with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Transporte> transporteCollectionOrphanCheck = estadoespera.getTransporteCollection();
            for (Transporte transporteCollectionOrphanCheckTransporte : transporteCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Estadoespera (" + estadoespera + ") cannot be destroyed since the Transporte " + transporteCollectionOrphanCheckTransporte + " in its transporteCollection field has a non-nullable estadoespera field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Detallerecepcion> detallerecepcionCollection = estadoespera.getDetallerecepcionCollection();
            for (Detallerecepcion detallerecepcionCollectionDetallerecepcion : detallerecepcionCollection) {
                detallerecepcionCollectionDetallerecepcion.setEstadoespera(null);
                detallerecepcionCollectionDetallerecepcion = em.merge(detallerecepcionCollectionDetallerecepcion);
            }
            em.remove(estadoespera);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Estadoespera> findEstadoesperaEntities() {
        return findEstadoesperaEntities(true, -1, -1);
    }

    public List<Estadoespera> findEstadoesperaEntities(int maxResults, int firstResult) {
        return findEstadoesperaEntities(false, maxResults, firstResult);
    }

    private List<Estadoespera> findEstadoesperaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Estadoespera as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Estadoespera findEstadoespera(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Estadoespera.class, id);
        } finally {
            em.close();
        }
    }

    public int getEstadoesperaCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Estadoespera as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

    public Estadoespera findEstadoespera_x_nombre(String nom) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select u from Estadoespera u where u.nombreEstadoEspera = " + "'" + nom + "'");
            return (Estadoespera) q.getSingleResult();
        } finally {
            em.close();
        }
    }
}
